import {
    createRouter,
    createWebHashHistory,
    createWebHistory,
    Router,
    RouteRecordRaw,
} from "vue-router";

export const ROUTER_NAME_HOME = "index"; //首页
export const ROUTER_NAME_SELECT_IMG_PAGE = "selectImgPage"; //选择图片页面
export const ROUTER_NAME_CLIP_PAGE = "clipPage"; //裁剪页面
export const ROUTER_NAME_RECORD_PAGE = "recordPage"; //历史记录页面
export const ROUTER_NAME_PRODUCE_VIDEO_PAGE = "produceVideoPage"; //视频生成中
export const ROUTER_NAME_PREVIEW_PAGE = "previewPage"; //预览
export const ROUTER_NAME_FAILED_PAGE = "failedPage"; //生成失败
export const ROUTER_NAME_NOT_FOUND = "404"; //404

//定义的路由必须和router中的path 一致  routerPath 数组中没有的将跳转登录页面，所以页面的路径需要在这里配置
const routerPath: string[] = [
    "/",
    "/crbt",
    "/" + ROUTER_NAME_SELECT_IMG_PAGE,
    "/" + ROUTER_NAME_CLIP_PAGE,
    "/" + ROUTER_NAME_RECORD_PAGE,
    "/" + ROUTER_NAME_PRODUCE_VIDEO_PAGE,
    "/" + ROUTER_NAME_PREVIEW_PAGE,
    "/" + ROUTER_NAME_FAILED_PAGE,
];

const mainRouters: Array<RouteRecordRaw> = [
    {
        path: "/", //首页
        name: ROUTER_NAME_HOME,
        meta: {
            title: "AI 3D视频彩铃",
        },
        component: () => import("@/views/home/index.vue"),
    },
    {
        path: "/" + ROUTER_NAME_SELECT_IMG_PAGE, //选择图片
        name: ROUTER_NAME_SELECT_IMG_PAGE,
        meta: {
            title: "选择图片",
        },
        component: () => import("@/views/home/selectImgPage.vue"),
    },
    {
        path: "/clipPage", //裁剪
        name: ROUTER_NAME_CLIP_PAGE,
        meta: {
            title: "选择图片",
        },
        component: () => import("@/views/home/clipPage.vue"),
    },
    {
        path: "/" + ROUTER_NAME_RECORD_PAGE, //记录
        name: ROUTER_NAME_RECORD_PAGE,
        meta: {
            title: "生成记录",
        },
        component: () => import("@/views/home/recordPage.vue"),
    },
    {
        path: "/" + ROUTER_NAME_PRODUCE_VIDEO_PAGE, //生成视频中page
        name: ROUTER_NAME_PRODUCE_VIDEO_PAGE,
        meta: {
            title: "AI 3D视频彩铃",
        },
        component: () => import("@/views/home/produceVideoPage.vue"),
    },
    {
        path: "/" + ROUTER_NAME_PREVIEW_PAGE, //记录
        name: ROUTER_NAME_PREVIEW_PAGE,
        meta: {
            title: "AI 3D视频彩铃",
        },
        component: () => import("@/views/home/previewPage.vue"),
    },
    {
        path: "/" + ROUTER_NAME_FAILED_PAGE, //生成失败
        name: ROUTER_NAME_FAILED_PAGE,
        meta: {
            title: "AI 3D视频彩铃",
        },
        component: () => import("@/views/home/failedPage.vue"),
    },
];

const errorRouter: Array<RouteRecordRaw> = [
    {
        path: "/404",
        name: "404",
        meta: {
            title: "404",
        },
        component: () => import("@/views/other/404.vue"),
    },
];

const router = createRouter({
    history: createWebHistory(),
    // history: createWebHashHistory(),
    routes: [...mainRouters, ...errorRouter],
    //跳转到新页面时 滑动到屏幕开始的位置
    scrollBehavior: (to, from, savedPosition) => {
        return savedPosition;
    },
});

router.beforeEach((to, from, next) => {
    if (to.meta.title) {
        (document.title as any) = to.meta.title;
    }

    if (window.location.search.indexOf("mgCallback") > -1) {
        sessionStorage.setItem("locationSearch", window.location.search);
    }
    // if (to.path === "/") {
    //     next("/crbt");
    //     return;
    // }

    // 不存在的页面 跳转到新登录页面
    if (routerPath.indexOf(to.path as string) === -1) {
        next("/crbt");
        return;
    }

    if (to.fullPath === "/404") {
        next();
        return;
    }
    next();
});

export default router;
